package com.work.keep.refresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.work.keep.R;

/**
 * Created by dllo on 16/1/17.
 */
public class MyRefreshLayoutManager extends StateLayoutManager {
    //状态提示栏 dateHeader内容提要 dateHeader内容正文
    private TextView headerStatusAlert, timeHeader, dateHeader;
    private ImageView headerArrow;
    private ProgressBar progressBar;
    private Animation myUpAnimation;
    private Animation myDownAnimation;
    //旋转动画的持续时间
    private static final int ROTATE_ANIM_DURATION = 150;
    private RelativeLayout myRelativeContainer;


    public MyRefreshLayoutManager(Context context) {
        this(context, null);
    }

    public MyRefreshLayoutManager(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyRefreshLayoutManager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        headerStatusAlert = (TextView) findViewById(R.id.headerStatusAlert);
        headerArrow = (ImageView) findViewById(R.id.headerArrow);
        timeHeader = (TextView) findViewById(R.id.timeHeader);
        progressBar = (ProgressBar) findViewById(R.id.pull_to_refresh_header_progressbar);
        //自定义旋转动画
        float toDegree = -180f;
        float pivotValue = 0.5f;
        myUpAnimation = new RotateAnimation(0.0f, toDegree, Animation.RELATIVE_TO_SELF, pivotValue, Animation.RELATIVE_TO_SELF, pivotValue);
        myUpAnimation.setDuration(ROTATE_ANIM_DURATION);
        myUpAnimation.setFillAfter(true);
        myDownAnimation = new RotateAnimation(toDegree, 0.0f, Animation.RELATIVE_TO_SELF, pivotValue, Animation.RELATIVE_TO_SELF, pivotValue);
        myDownAnimation.setDuration(ROTATE_ANIM_DURATION);
        myDownAnimation.setFillAfter(true);
    }

    @Override
    protected View createContainView(Context context, AttributeSet attrs) {
        myRelativeContainer = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.layout_head_one, null);
        return myRelativeContainer;
    }

    //该方法只针对于上拉加载
    @Override
    protected void onNoMoreData() {

    }

    @Override
    protected void onRefreshing() {
        //1进入刷新状态 2要让指示箭头消失 3提示正在刷新
        //1
        headerArrow.setVisibility(INVISIBLE);
        //2让progressBar显示
        progressBar.setVisibility(VISIBLE);
        //3 提示正在刷新
        headerStatusAlert.setText("正在刷新");
    }

    @Override
    protected void onPullToRefresh() {
        //从松开到没有进入onRefresh方法 再次拉住时
        //冷却
        if (LoadState.RELEASE_TO_REFRESH == getPreState()) {
            //这里不清理动画 直接执行down动画
            headerArrow.clearAnimation();
            headerArrow.startAnimation(myDownAnimation);
            //
        }
        //从正在刷新状态被再次拉下时
        if (LoadState.REFRESHING == getPreState()) {
            headerStatusAlert.setText("Already refreshing! Wait,plese.");
        } else {
            //最初的状态就是箭头向下的 不需要开启动画
//            headerArrow.setAnimation(myDownAnimation);
            headerStatusAlert.setText("下拉可以刷新");
        }
    }


    //已经做好松开 刷新的准备
    //这时候我们需要准备两件事 一个是让文字提示变成松开可以刷新 第二件事是
    @Override
    protected void onReadyForReleaseToRefresh() {
//        headerArrow.clearAnimation();
        //// TODO: 16/1/18  我们先不让在松开的时候变为刷新状态
        headerStatusAlert.setText("松开可以刷新");
        //为什么每次都执行清除动画
        headerArrow.clearAnimation();
        headerArrow.startAnimation(myUpAnimation);
    }

    //重置
    @Override
    protected void onReset() {
        //清除箭头动画 原因 这是循环的结束也是循环的开始 要将箭头位置 在我们看不到的时候 清除之前的动画
        headerArrow.clearAnimation();
        //将状态提示栏恢复为下来可以刷新
        headerStatusAlert.setText("下来可以刷新");
    }

    //获取其尺寸的方法
    @Override
    public int getContentSize() {
        if (myRelativeContainer != null) {
            return myRelativeContainer.getHeight();
        }
        //TODO 这是个啥?
        return (int) (getResources().getDisplayMetrics().density * 60);
    }

    //生成更新时间的方法
    @Override
    public void setLastUpdatedLabel(CharSequence label) {
        super.setLastUpdatedLabel(label);
        dateHeader.setText(label);
    }

    @Override
    protected void onStateChanged(LoadState curState, LoadState preState) {
        //Todo 保持progressBar 和 headerArrow进入这个方法的时候的显隐
        headerArrow.setVisibility(VISIBLE);
        progressBar.setVisibility(INVISIBLE);
        super.onStateChanged(curState, preState);
    }
}
